<template>
  <div>
    <!-- 面包屑 -->
    <MBX level1="权限管理" level2="角色列表"></MBX>
    <!-- 卡片视图 -->
    <el-card class="mt15">
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <el-table border class="mt15" :data="list">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <div class="expand_box">
              <div v-if="scope.row.children.length > 0">
                <el-row
                  class="borderBtm_1px"
                  v-for="item1 in scope.row.children"
                  :key="item1.id"
                >
                  <el-col :span="6">
                    <el-tag
                      @close="
                        deleteRoleRights(scope.row, scope.row.id, item1.id)
                      "
                      closable
                      >{{ item1.authName }}</el-tag
                    >
                  </el-col>
                  <el-col :span="18">
                    <el-row
                      class="borderBtm_1px"
                      v-for="item2 in item1.children"
                      :key="item2.id"
                    >
                      <el-col :span="6">
                        <el-tag
                          @close="
                            deleteRoleRights(scope.row, scope.row.id, item2.id)
                          "
                          type="success"
                          closable
                          >{{ item2.authName }}</el-tag
                        >
                      </el-col>
                      <el-col :span="18">
                        <el-tag
                          @close="
                            deleteRoleRights(scope.row, scope.row.id, item3.id)
                          "
                          closable
                          type="warning"
                          v-for="item3 in item2.children"
                          :key="item3.id"
                        >
                          {{ item3.authName }}
                        </el-tag>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </div>
              <div v-else>该角色暂无权限</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div>
              <el-button type="primary" size="mini" icon="el-icon-edit"
                >编辑</el-button
              >
              <el-button type="danger" size="mini" icon="el-icon-delete"
                >删除</el-button
              >
              <el-button
                type="warning"
                size="mini"
                icon="el-icon-setting"
                @click="setRightsDia(scope.row)"
                >分配权限</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="isSetRights"
      width="30%"
      :before-close="handleClose"
    >
      <!-- 
      tree组件参数含义：
      data:数据源
      show-checkbox：是否有复选框
      node-key:tree组件的唯一标识
      default-expand-all:是否全部展开
      default-expanded-keys：[2,4,5 ]只展开给出的id节点的数组
      default-checked-keys:[3232,23,23,23,23,23,23],设置选中的id组成的数组
      props:配置数据源的选项  label:设置显示的名称,children:设置子级属性名称
     -->
      <el-tree
        ref='treeRef'
        :data="treeList"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="checkArr"
        :props="defaultProps"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isSetRights = false">取 消</el-button>
        <el-button type="primary" @click="updateRoleRightsOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //当前角色id
      currentRoleId:'',
      //已选中的权限id组成的数组
      checkArr: [],
      //默认隐藏权限对话框
      isSetRights: false,
      list: [],
      treeList: [],
      defaultProps: {
        children: "children",
        label: "authName",
      },
    };
  },
  methods: {
    //确认更新角色的权限列表
   async updateRoleRightsOk() {
      //1.获取全选的id组成的数组
      const allCheckedsIds=this.$refs.treeRef.getCheckedKeys()
      //2.获取半选id组成的数组
      const halfCheckedsIds=this.$refs.treeRef.getHalfCheckedKeys()

      //3.合并全选和半id组成的数组
      const mergeCheckedArr=[...allCheckedsIds,...halfCheckedsIds]

      //将合并的数组转换成字符串  数组转为字符串：join()，字符串转数组：split()
      const AllcheckedIdsStr=mergeCheckedArr.join(',')
      //调用更新权限的接口将最新选中的权限传递给后台，从而更新当前角色的权限
      const res=await this.$API.updateRoleRightsAPI(this.currentRoleId,AllcheckedIdsStr)
       
      //更新权限列表
      this.xr()

      //隐藏分配权限对话框
      this.isSetRights = false;

    },
    //监听关闭分配权限对话框
    handleClose() {
      console.log("监听关闭分配权限对话框");
      this.checkArr = [];
      this.isSetRights = false;
    },
    //打开分配权限对话框
    async setRightsDia(row) {
      //清空上次选择的结果
      this.checkArr = [];
      //获取当前roleid
      this.currentRoleId=row.id
      //显示分配权限对话框
      this.isSetRights = true;

      //调用树形权限接口
      const res = await this.$API.getRightsListAPI("tree");

      //将获取的权限列表赋值给treeList
      this.treeList = res;
      
      //递归获取权限方法
      this.getCheckedIds(row, this.checkArr);

      console.log("id数组：", this.checkArr);
    },
    //递归获取权限id方法
    getCheckedIds(row, checkArr) {
      //没有children,就代表已经是最后一项，如果是最后一项，则把它的id，push给一个数组
      if (!row.children) {
        return checkArr.push(row.id);
      }

      row.children.forEach((item) => {
        this.getCheckedIds(item, checkArr);
      });
    },
    //删除角色指定的权限
    async deleteRoleRights(row, roleId, rightId) {
      //调删除删除角色指定的权限接口，返回最新权限列表
      const res = await this.$API.deleteRoleRightsAPI(roleId, rightId);

      //更新当前角色权限
      row.children = res;
      //更新角色列表
      // this.xr();
    },
    async xr() {
      const res = await this.$API.getRoleListAPI();
      console.log("角色列表：", res);
      this.list = res;
    },
  },
  created() {
    this.xr();
  },
};
</script>

<style lang="scss" scoped>
.el-tag {
  margin: 10px 20px;
}

.expand_box {
  padding: 0 40px;
}
.borderBtm_1px {
  border-bottom: 1px solid #eee;
}
</style>
